package kz.vokebuz.dataStore;

import java.text.SimpleDateFormat;

import kz.vokebuz.R;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class SmsSimplCursorAdapter extends SimpleCursorAdapter {
	private final Context mContext;
	private final int mLayout;
	private final Cursor mCursor;
	private final LayoutInflater mLayoutInflater;
	LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(100, 60);

	private final class ViewHolder {
		public TextView sender;
		public ImageView image;
		public TextView textmsg;
	}

	public SmsSimplCursorAdapter(Context context, int layout, Cursor c,
			String[] from, int[] to) {
		super(context, layout, c, from, to);
		this.mContext = context;
		this.mLayout = layout;
		this.mCursor = c;
		this.mLayoutInflater = LayoutInflater.from(mContext);

	}

	public View getView(int position, View convertView, ViewGroup parent) {
		if (mCursor.moveToPosition(position)) {
			ViewHolder viewHolder;

			if (convertView == null) {
				convertView = mLayoutInflater.inflate(mLayout, null);
				viewHolder = new ViewHolder();
				viewHolder.sender = (TextView) convertView
						.findViewById(R.id.rowSender);
				viewHolder.textmsg = (TextView) convertView
						.findViewById(R.id.rowText);
				viewHolder.image = (ImageView) convertView
						.findViewById(R.id.icon);

				convertView.setTag(viewHolder);

			} else {
				viewHolder = (ViewHolder) convertView.getTag();

			}
			String strdate = new SimpleDateFormat("yyyy-MM-dd hh:mm")
					.format(mCursor.getLong(mCursor
							.getColumnIndex(SmsMmsMsg.COLUMN_TIMESTAMP)));
			viewHolder.sender.setText(mCursor.getString(mCursor
					.getColumnIndex(SmsMmsMsg.COLUMN_SENDER)) + " " + strdate);
			viewHolder.textmsg.setText("(" 
				+ mCursor.getInt(mCursor.getColumnIndex("countmsg"))
				+ ")   " 
				+ 	mCursor.getString(mCursor.getColumnIndex(SmsMmsMsg.COLUMN_TEXT)));
			// viewHolder.textmsg.setLayoutParams(lp);
			viewHolder.image.setImageResource(R.drawable.icon);

		}
		return convertView;
	}
}
